import pymysql
from dbutils.pooled_db import PooledDB

from config import mysql_conf

class MySQLPool(object):
    # 类属性
    pool = PooledDB(creator=pymysql,**mysql_conf) # 注意creator参数

    def __enter__(self):
        self.conn = MySQLPool.pool.connection()
        self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        # 关闭链接
        self.cursor.close()
        self.conn.close()